***
set scheme s1manual
clear
pause on
set more off
do ${stata_code}/taxtax_define_specs.do

*global results = "C:\Users\alexander.smith\OneDriveWP\research\projects\EITC\results"

use "${results}/mechrd_graph", clear
gen age = t
append using "${results}/mainrd_graph"

sort outcome sample age taxyrcut bin_z

gen samp_elig= 1
replace samp_elig= 0 if strmatch(sample,"*noelig*")==1

global measure = "mean"
*local ys = " pwagez3 " //pwagez3 i_pov3
levelsof outcome , local(ys)

**Graph Sensitivity (round up to nearest m from max and down to nearest m from min -- tries to be consistent across specification changes for same outcome)
global m_wagez3 = 2000
global m_pwagez3 = 2
global m_male = .05
global m_parent_wagez = 5000
global m_i_pov0 = .05
global m_parent_i_1040 = .05
global m_i_married0 = .05
global m_tyrc_prdeitc = 200
global m_tyrc_prdtax_taxsimL = 500
global m_tyrc_pragiz = 2000
global m_tyrc_maxage = 1
global m_black = .05
global m_hisp = .05
global m_white_non_hisp = .1
*global m_i_pov3 = .05

capture noisily mkdir  "${results}/rdgraphs"
capture noisily mkdir  "${results}/rdgraphs/nodonut"

foreach y of local ys {
	di "*****Y = `y'"
	levelsof age if outcome=="`y'" , local(ages)
	foreach age of local ages {
		di "****age = `age'"

		summ ${measure} if outcome=="`y'" & age=="`age'"  & strmatch(sample,"*noelig*")==0
		local ymax_elig = ceil(r(max)/${m_`y'})*${m_`y'}
		local ymin_elig = floor(r(min)/${m_`y'})*${m_`y'}

		summ ${measure} if outcome=="`y'" & age=="`age'"  & strmatch(sample,"*noelig*")==1
		local ymax_noelig = ceil(r(max)/${m_`y'})*${m_`y'}
		local ymin_noelig = floor(r(min)/${m_`y'})*${m_`y'}

		disp "****max/min***"
		disp "elig: `ymax_elig'  / `ymin_elig'"
		disp "inelig: `ymax_noelig'  / `ymin_noelig'"

		levelsof sample if outcome=="`y'" & age=="`age'", local(samps)
		foreach samp of local samps {
		di "***samp = `samp'"

		local e = "elig"
		if strmatch("`samp'","*noelig*")==1 local e = "noelig"

			levelsof taxyrcuts if outcome=="`y'" & sample=="`samp'" & age=="`age'", local(taxyrcuts)
			foreach tyrc of local taxyrcuts {
				di "**taxyrcut = `tyrc'"
				levelsof predsamps if outcome=="`y'" , local(predsamps)
				foreach predsamp of local predsamps {
					di "*predsamp = `predsamp'"
						disp "**"
						local w = ""
						*
						if "${measure}"=="mean" {
							local w = "[aw=obs]"
						}
						*
						capture drop yrange

						local r = `" inrange(${measure},`ymin_`e'',`ymax_`e'') & sample=="`samp'" & predsamps=="`predsamp'" & outcome=="`y'" & age=="`age'" & taxyrcuts=="`tyrc'" "'
						capture noisily gen y_d8 = .
						replace y_d8 = .
						replace y_d8 = ${measure} if `r' & inrange(bin_z,-28,28) & !inrange(bin_z,-8,7)

						disp "******************************************************"
						disp "`y' / `age' / `tyrc' / `samp' (`e')/ `predsamp' (DONUT)"
						disp `"restriction: `r'"'

					capture noisily {
						disp "Graphed"
						graph twoway scatter ${measure} bin_z if `r' & inrange(bin_z,-30,30) & !inrange(bin_z,-8,7) ///
										, msymbol(O) xline(-8.5 7.5, lpattern(dash) lcolor(gs11) ) xline(0, lpattern(dash) lcolor(black) ) ///
							|| lfit y_d8 bin_z `w' if `r' & inrange(bin_z,-30,-1) , lcolor(black) range(-30 -1) ///
							|| lfit y_d8  bin_z `w' if `r' & inrange(bin_z,0,30) , lcolor(black) range(0 30) ///
							legend(off) xlabel(#6) ylabel(#6) xtitle("Birthdate (Centered at Jan 1)") ytitle("") ///
							yscale(range(`ymin_`e''  `ymax_`e''))
					}
					capture graph export "${results}/rdgraphs/`y'_`samp'_`age'.png" , replace

					disp "`y' / `age' / `tyrc' / `samp' (`e')/ `predsamp' (NODONUT)"
					disp `"restriction: `r'"'

					capture noisily {
						disp "Graphed"
						graph twoway scatter ${measure}  bin_z if `r' & inrange(bin_z,-30,30) & !inrange(bin_z,-8,7), msymbol(O) xline(-8.5 7.5, lpattern(dash) lcolor(gs11) ) xline(0, lpattern(dash) lcolor(black) ) ///
							|| scatter ${measure}  bin_z if `r'  & inrange(bin_z,-8,7) , msymbol(Oh) mcolor(black)  ///
							|| lfit ${measure}  bin_z `w' if `r' & inrange(bin_z,-30,-1) , lcolor(black) ///
							|| lfit ${measure}  bin_z `w' if `r' & inrange(bin_z,0,30) , lcolor(black) ///
							legend(off) legend(off) xlabel(#6) ylabel(#6) xtitle("Birthdate (Centered at Jan 1)") ytitle("") ///
							yscale(range(`ymin_`e''  `ymax_`e''))
					}
					capture graph export "${results}/rdgraphs/nodonut/`y'_`samp'_`age'_nod.png" , replace
					disp "******************************************************"
					disp ""
				}
			}
		}
	}
}
